<template>
  <div>
    <div class="gva-table-box">
      <div>
        <el-form :inline="true" :model="searchData">
          <el-form-item label="团队名称">
            <el-input v-model.trim="searchData.teamName" placeholder="搜索条件"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="search" @click="getList">查询</el-button>
            <el-button icon="refresh" @click="clear">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="gva-btn-list">
        <el-button type="primary" icon="plus" @click="openAdd">新增团队</el-button>
      </div>
      <el-table :data="tableData" row-key="ID" v-loading="loading">
        <el-table-column align="left" label="团队编号" min-width="120" prop="teamId"/>
        <el-table-column align="left" label="团队名称" min-width="115" prop="teamName"/>
        <el-table-column align="left" label="创建时间" min-width="130" prop="createTime"/>
        <el-table-column align="left" label="修改时间" min-width="130" prop="updateTime"/>
        <el-table-column label="操作" min-width="220" fixed="right">
          <template #default="scope">
            <el-button type="primary" link icon="ChatLineRound" @click="openInfo(scope.row)">详情</el-button>
            <el-button type="primary" link icon="edit" @click="openEdit(scope.row)">编辑</el-button>
            <el-popover v-model="scope.row.visible" placement="top" width="160">
              <p>确定要删除吗?</p>
              <div style="text-align: right; margin-top: 8px;">
                <el-button type="primary" link @click="scope.row.visible = false">取消</el-button>
                <el-button type="primary" @click="deleteFunc(scope.row)">确定</el-button>
              </div>
              <template #reference>
                <el-button type="danger" link icon="delete">删除</el-button>
              </template>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <div class="gva-pagination">
        <el-pagination :current-page="page" :page-size="pageSize" :page-sizes="[10, 30, 50, 100]"
                       :total="total" layout="total, sizes, prev, pager, next, jumper"
                       :hide-on-single-page="pageSize===10"
                       @current-change="handleCurrentChange" @size-change="handleSizeChange"/>
      </div>
    </div>
    <!--    新增修改模态框-->
    <el-dialog v-model="openDialog" :title="dialogFlag+'团队'" :show-close="false"
               :close-on-press-escape="false" :close-on-click-modal="false"
               draggable width="500px">
      <div style="overflow:auto;padding:0 12px;">
        <el-form ref="from" :model="teamInfo" label-width="80px">
          <el-form-item v-if="dialogFlag === '修改'" label="团队编号" prop="userName">
            <el-input readonly="readonly" v-model.trim="teamInfo.teamId"/>
          </el-form-item>
          <el-form-item label="团队名称" prop="name">
            <el-input v-model.trim="teamInfo.teamName"/>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="closeDialog()">取 消</el-button>
          <el-button type="primary" @click="editOrAdd()">确 定</el-button>
        </div>
      </template>
    </el-dialog>
    <!--    详情模态框-->
    <el-dialog v-model="infoDialog" :header="'团队详情'"
               :close-on-press-escape="false" :close-on-click-modal="false"
               draggable width="500px">
      <el-form ref="from" :model="teamInfo" label-width="80px">
        <el-form-item label="团队编号" prop="userName">
          <el-input readonly="readonly" v-model.trim="teamInfo.teamId"/>
        </el-form-item>
        <el-form-item label="团队名称" prop="name">
          <el-input v-model.trim="teamInfo.teamName" readonly="readonly"/>
        </el-form-item>
        <el-form-item label="团队成员" prop="name">
          <template v-if="teamInfo.employeesList.length===0">
            暂无成员
          </template>
          <el-tag v-for="item in teamInfo.employeesList" style="margin-right: 3px">{{ item.name }}</el-tag>
        </el-form-item>
      </el-form>

    </el-dialog>
  </div>
</template>

<script setup>
import {ref} from 'vue'
import {ElMessage} from "element-plus";
import {delTeam, editTeam, getTeamInfo, getTeamList, saveTeam} from "@/api/team";
import {employeesList} from "@/api/authority";
//data
const tableData = ref()
const teamInfo = ref({
  teamId: '',
  teamName: '',
  employeesList: []
})
const from = ref()
const openDialog = ref(false)
const infoDialog = ref(false)

const loading = ref(true)
const dialogFlag = ref()
//page
const total = ref(0)
const page = ref(1)
const pageSize = ref(10)
const searchData = ref({teamName: ''})

// 重置按钮
const clear = () => {
  searchData.value = {teamName: ''}
  getList()
}

// 分页
const handleSizeChange = (val) => {
  pageSize.value = val
  getList()
}
const handleCurrentChange = (val) => {
  page.value = val
  getList()
}
//查询
const getList = async () => {
  loading.value = true
  const res = await getTeamList({page: page.value, pageSize: pageSize.value, ...searchData.value})
  if (res.code === 0) {
    tableData.value = res.data.records
    total.value = +res.data.total
    loading.value = false
  }
}
//删除
const deleteFunc = async (row) => {
  const res = await delTeam(row.teamId)
  if (res.code === 0) {
    ElMessage({type: 'success', message: res.msg})
  }
  row.visible = false
  await getList()
}
//修改
const openEdit = async (row) => {
  const res = await getTeamInfo(row.teamId)
  teamInfo.value = JSON.parse(JSON.stringify(res.data))
  dialogFlag.value = '修改'
  openDialog.value = true
}
//新增
const openAdd = () => {
  dialogFlag.value = '新增'
  openDialog.value = true
}
const openInfo = async (row) => {
  const res = await getTeamInfo(row.teamId)
  teamInfo.value = JSON.parse(JSON.stringify(res.data))
  infoDialog.value = true
}
//新增或修改请求
const editOrAdd = async () => {
  let res
  const req = {...teamInfo.value}
  if (dialogFlag.value === '新增') {
    res = await saveTeam(req)
  } else {
    res = await editTeam(req)
  }
  if (res.code === 0) {
    ElMessage({type: 'success', message: res.msg})
    await closeDialog()
  }
}
//关闭模态框
const closeDialog = async () => {
  openDialog.value = false
  initForm()
  await getList()
}
//初始化表单
const initForm = () => {
  if (from.value) {
    from.value.resetFields()
  }
  teamInfo.value = {}
  dialogFlag.value = ''
}

getList()

</script>
<style scoped lang="scss"></style>
